package service

import (
	"web_framework/pkg/constvar"
	"web_framework/storage"

	. "web_framework/pkg/e"
)

//获取单个连接设备的详情
//func getJoinDeviceInfo(sn string, mac string)(model.Device, error){
//
//}

//获取离线在线设备列表
func GetOfflineDeviceList(sn string)(map[string]map[string]interface{}, error){
	listKey := constvar.RouterRedisPrefix + sn + storage.RedisJoinDeviceOfflineListKey
	result, err := storage.RedisZRange(listKey, 0, 20)
	if err != nil {
		Log("Info", "获取redis数据失败", err.Error())
		return nil, err
	}
	var deviceListInfo = make(map[string]map[string]interface{})
	for _, value := range result {
		deviceUUID := Md5Str(sn+value)
		deviceInfoCacheKey := constvar.RouterRedisPrefix + sn + storage.RedisJoinDeviceInfoKey + ":" + deviceUUID
		res, err := storage.RedisHGetAll(deviceInfoCacheKey)
		if err != nil {
			Log("Info", "任务设备详情失败")
		}
		if mac,ok := res["mac"];ok{
			deviceListInfo[mac.(string)] = res
		}
	}

	return deviceListInfo, nil
}

//获取在线设备列表详情
func GetOnlineDeviceList(sn string)(map[string]map[string]interface{}, error){
    listKey := constvar.RouterRedisPrefix + sn + storage.RedisJoinDeviceOnlineListKey
	result, err := storage.RedisZRange(listKey, 0, 20)
	if err != nil {
		return nil, err
	}
	var deviceListInfo = make(map[string]map[string]interface{})
	for _, value := range result {
		deviceUUID := Md5Str(sn+value)
		deviceInfoCacheKey := constvar.RouterRedisPrefix + sn + storage.RedisJoinDeviceInfoKey + ":" + deviceUUID
		res, err := storage.RedisHGetAll(deviceInfoCacheKey)
		if err != nil {
			Log("Info", "任务设备详情失败", err.Error())
		}
		if mac,ok := res["mac"];ok{
			deviceListInfo[mac.(string)] = res
		}
	}

	return deviceListInfo, nil
}
